#!/bin/bash

source /home/shell/util.sh



# 安装函数
function install () {
    while [ true ]; do
        curl -sL https://www.elastic.co/cn/downloads/beats/filebeat#ga-release|grep -Po "filebeat-\d+\.\d+\.\d-linux-x86_64.tar.gz"|uniq
        read -p "请选择版本：" version
        if [[ -z "${version}" ]]; then
            printFail "请输入版本"
        else
            break;
        fi
    done
    download /opt/filebeat.tar.gz https://artifacts.elastic.co/downloads/beats/filebeat/${version}
    tar -xf /opt/filebeat.tar.gz -C /opt
    rm -rf /opt/filebeat.tar.gz
    mv /opt/filebeat-* /opt/filebeat
}

# 配置函数
function config(){

    read -p "请输入kibana 主机[${host_addr}]：" kibana_host
    kibana_host=${kibana_host:-"${host_addr}"};

    read -p "请输入elasticsearch 主机[${host_addr}]：" elasticsearch_host
    elasticsearch_host=${elasticsearch_host:-"${host_addr}"};

    read -p "请输入elasticsearch 用户名[elastic]：" elasticsearch_username
    elasticsearch_username=${elasticsearch_username:-"elastic"};

    read -p "请输入elasticsearch 密码：" elasticsearch_passwd

    sed -ri "/setup.kibana:/a\  host: \""${kibana_host}":5601\"" /opt/filebeat/filebeat.yml
    sed -ri "s/hosts:\s*\[\"localhost:9200\"\]/hosts: \[\""${elasticsearch_host}":9200\"\]/ig" /opt/filebeat/filebeat.yml
    sed -ri "s/#username: \"elastic\"/username: \""${elasticsearch_username}"\"/ig" /opt/filebeat/filebeat.yml
    sed -ri "s/#password: \"changeme\"/password: \""${elasticsearch_passwd}"\"/ig" /opt/filebeat/filebeat.yml
    sed -ri "s/#setup.dashboards.enabled: false/setup.dashboards.enabled: true/ig" /opt/filebeat/filebeat.yml

    /opt/filebeat/filebeat --path.config=/opt/filebeat test output
    /opt/filebeat/filebeat --path.config=/opt/filebeat setup --dashboards
    # /opt/filebeat/filebeat -e -c /opt/filebeat/filebeat.yml -d "publish"
}

function run () {
bash /home/shell/serviced-install.sh<<EOF



filebeat
/opt/filebeat/filebeat -e -c /opt/filebeat/filebeat.yml -d "publish"




EOF
}

# 运行安装和配置
install && config && run